/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.imageio.ImageIO;
import util.Package;

/**
 *
 * @author Vlada, Aleksandar
 */
public class Server {

    static int port = 3070;

    public static void main(String[] args) {

        System.out.println("*** Server je pokrenut");

        try {
            InetAddress ip = InetAddress.getLocalHost();
            System.out.println("*** Vreme: " + date() + " *** IP Adresa servera je: " + ip.getHostAddress());
        } catch (UnknownHostException ex) {
            System.err.println(ex.getMessage());
        }

        try {
            ServerSocket serverSocket = new ServerSocket(port);
            Socket socket = serverSocket.accept();
            ServerThread serverThread = new ServerThread();
            serverThread.start();

            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            int wServer = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
            int hServer = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();

            oos.writeInt(wServer);
            oos.writeInt(hServer);
            oos.flush();

            int wClient = ois.readInt();
            int hClient = ois.readInt() - 60;

            Robot robot = new Robot();

            BufferedImage screenSnap = null;
            byte[] byteArray;
            ByteArrayOutputStream byteArrayOS;
            int bytesOfLastScreen = 0;

            boolean resize1 = false;
            boolean resize2 = false;
            if (wServer > wClient && hServer > hClient) {
                resize1 = true;
                if (wServer - wClient > hServer - hClient) {
                    resize2 = true;
                }
            }

            int wFinal = 0;
            int hFinal = 0;

            double percent = 100.0;

            if (!resize1) {
                wFinal = wServer;
                hFinal = hServer;
            } else {
                if (resize2) {
                    percent = wClient * 100 / wServer;
                } else {
                    percent = hClient * 100 / hServer;
                }
                wFinal = (int) (wServer * (percent / 100));
                hFinal = (int) (hServer * (percent / 100));
            }

            oos.writeInt(wFinal);
            oos.writeInt(hFinal);
            oos.writeDouble(percent);
            oos.flush();

            while (true) {
                if (!resize1) {
                    screenSnap = robot.createScreenCapture(new Rectangle(wServer, hServer));
                } else {
                    Image image = robot.createScreenCapture(new Rectangle(wServer, hServer)).getScaledInstance(wFinal, hFinal, BufferedImage.SCALE_SMOOTH);
                    screenSnap = new BufferedImage(wFinal, hFinal, BufferedImage.TYPE_INT_RGB);
                    screenSnap.getGraphics().drawImage(image, 0, 0, null);
                }
                byteArrayOS = new ByteArrayOutputStream();
                ImageIO.write(screenSnap, "JPG", byteArrayOS);
                byteArray = byteArrayOS.toByteArray();
                if (bytesOfLastScreen != byteArray.length) {
                    bytesOfLastScreen = byteArray.length;
                    oos.writeInt(byteArray.length);
                    int numberOfPackages = byteArray.length / 1024;
                    int b = 0;
                    for (int j = 0; j < numberOfPackages; j++) {
                        byte[] packageOfBytes = Arrays.copyOfRange(byteArray, j * 1024, (j + 1) * 1024);
                        Package pob = new Package(packageOfBytes);
                        oos.writeObject(pob);
                        b = j + 1;
                    }
                    byte[] restOfBytes = Arrays.copyOfRange(byteArray, b * 1024, byteArray.length);
                    oos.writeObject(new Package(restOfBytes));
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    System.err.println(ex.getMessage());
                }
            }

        } catch (AWTException | IOException ex) {
            System.err.println(ex.getMessage());
        }
    }

    public static String date() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        return (dateFormat.format(date)).toString();
    }
}